from django.urls import re_path
from django.conf import settings
from stark.service.v1 import StarkHandler, Option, get_choice_text
from web import models


class HostRecordHandler(StarkHandler):
    list_display = [
        get_choice_text('操作类型', 'operate_type'),
        'ip_address',
        'depart', 'app_name',
        'location',
        'note',
        'operate_person',
        'operate_date']

    def get_urls(self):
        """
        二次路由分发
        :return:
        """
        patterns = [
            re_path(r'^list/(?P<host_id>\d+)/$', self.wrapper(self.list_view), name=self.get_list_url_name),
            # re_path(r'^add/$', self.wrapper(self.add_view), name=self.get_add_url_name),
            # re_path(r'^change/(?P<pk>\d+)/$', self.wrapper(self.change_view),
            #         name=self.get_change_url_name),
            re_path(r'^delete/(?P<host_id>\d+)/(?P<pk>\d+)/$', self.wrapper(self.delete_view),
                    name=self.get_delete_url_name)
        ]
        patterns.extend(self.extra_url())
        return patterns

    def get_list_display(self, request, *args, **kwargs):
        """
        预留列表页面显示字段的钩子函数
        :return:
        """
        value = []
        if self.list_display:
            value.extend(self.list_display)
            permission_dict = request.session.get(settings.MOBILEDJ_PERMISSION_SESSION_KEY)
            if self.get_delete_url_name in permission_dict:
                value.append(type(self).display_delete)
        return value

    def get_add_btn(self, request, *args, **kwargs):
        return None

    search_group = [Option('depart'), ]
    search_list = ['ip_address__contains', 'app_name__contains', 'location__contains']

    def get_queryset(self, request, *args, **kwargs):
        ip_id = kwargs.get('host_id')
        return models.HostRecord.objects.filter(ip_id=ip_id)
